$(function () {
    initEvent();
});

function initEvent() {
    $("#payClick").off('click').on('click', function () {
        btn_Submit();
    });
}

var payObj,orderid;
var redirectUrl; // 用于支付首付款流程同步通知的流程

function btn_Submit() {
    var u_code = $("#payClick").attr("u_code");
    var order_num = $("#payClick").attr("order_num");

    //获取选择的支付方式
    var services = $("#services").val();
    var p_name = $("#p_name").val();
    var b_borrow_amount = $("#b_borrow_amount").val();

    //持牌金融机构账单
    redirectUrl = "/secoo/product_bill?p_name="+p_name+"&b_borrow_amount="+b_borrow_amount;

    if(services == 4){
        window.location.href = "/user/authform?channel=2&p_name="+p_name+"&b_borrow_amount="+b_borrow_amount;
    }
    //蚂蚁账单
    if(services == 5){
        //window.location.href = "/user/authform?channel=1";
        //修改流程，选择蚂蚁账单跳转到选择账期
        window.location.href = "/user/orderform?order_num="+order_num;
    }

    if(services == 4 || services == 5){
        return false;
    }

    if (!order_num && !u_code && !services) {
        falert('信息有误！');
        return false;
    }
    if (order_num && u_code && services) {
        var datas = {
            interface:'IcbcPay@yeeDeterminePay',
            order_num: order_num,
            u_code: u_code,
            services : services,
        };
        common.ajax({
            data: datas,
            success: function (data) {

                //工行支付（暂时不用）
                //if (data.status == '2000') {
                //    //支付成功
                //    document.write(data.result.pay);
                //}else if (data.status == '5003') {
                //    falert(data.msg);
                //}
                if (data.status == '2000') {

                    if(services == 1){//微信
                        payObj = {
                            "appId": data.result.pay.resultData.appId,         //公众号名称，由商户传入
                            "timeStamp": data.result.pay.resultData.timeStamp, //时间戳，自1970年以来的秒数
                            "nonceStr": data.result.pay.resultData.nonceStr, 	//随机串
                            "package": data.result.pay.resultData.package,		//统一下单接口返回的prepay_id参数值，提交格式如：prepay_id=1234
                            "signType": data.result.pay.resultData.signType,   //微信签名方式：
                            "paySign": data.result.pay.resultData.paySign 		//微信签名
                        };
                        orderid = data.result.pay.order_num;
                        //调用微信
                        callPay();
                    }else if(services == 2){//支付宝
                        window.location.href = data.result.pay;
                    }else if(services == 3){//银联
                        window.location.href = data.result.pay;
                    }
                }else {
                    alert(data.msg);
                }

            }
        });
    }
}


/**
 * 微信JSAPI调起
 */
function jsApiCall() {
    console.log(payObj);
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        payObj,
        function (res) {
            console.log(res);
            console.log(orderid);
            //支付成功
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                var redirUrl = "http://release.weixin.mayi121.com/paySuccess?order_num="+orderid;
                if($("#typePay").val() == 5 || $("#orderType").val() == 3){
                    redirUrl = redirectUrl;
                }
                window.location.href = redirUrl;
            } else if (res.err_msg == "get_brand_wcpay_request:fail") { //支付失败
                falert("支付失败2");
            } else if (res.err_msg == "get_brand_wcpay_request:cancel") { //支付过程中用户取消

            }
        });
}

/**
 * 调起支付
 */
function callPay() {
    console.log(1);
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', jsApiCall);
            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
        }
    } else {
        jsApiCall();
    }
}

